package com.example.day18_7_18_1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.day18_7_18_1.R;
import com.example.day18_7_18_1.bean.Bean;

import java.util.ArrayList;
import java.util.List;

public class InfoRclAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Bean.DataBean.DatasBean> list = new ArrayList<>();

    public void setList(List<Bean.DataBean.DatasBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public InfoRclAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1){
            View inflate = LayoutInflater.from(context).inflate(R.layout.layout_left, parent, false);
            return new HolderLeft(inflate);
        }
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_right, parent, false);
        return new HolderRight(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        int viewType = getItemViewType(position);
        if (viewType == 1){
            HolderLeft holderLeft = (HolderLeft) holder;
            holderLeft.tv_left.setText(list.get(position).getTitle());
            holderLeft.tv_id_left.setText(list.get(position).getId()+"");
            Glide.with(context).load(list.get(position).getEnvelopePic()).into(holderLeft.img_left);
            holderLeft.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClick != null){
                        onClick.onClick(position);
                    }
                }
            });
        }
        if (viewType == 2){
            HolderRight holderRight = (HolderRight) holder;
            holderRight.tv_right.setText(list.get(position).getTitle());
            holderRight.tv_id_right.setText(list.get(position).getId()+"");
            Glide.with(context).load(list.get(position).getEnvelopePic()).into(holderRight.img_right);
            holderRight.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClick != null){
                        onClick.onClick(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getId() % 3 == 0){
            return 2;
        }
        return 1;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class HolderLeft extends RecyclerView.ViewHolder {

        private ImageView img_left;
        private TextView tv_id_left;
        private TextView tv_left;

        public HolderLeft(@NonNull View itemView) {
            super(itemView);
            img_left = itemView.findViewById(R.id.img_left);
            tv_id_left = itemView.findViewById(R.id.tv_id_left);
            tv_left = itemView.findViewById(R.id.tv_left);
        }
    }
    public class HolderRight extends RecyclerView.ViewHolder {
        private ImageView img_right;
        private TextView tv_id_right;
        private TextView tv_right;

        public HolderRight(@NonNull View itemView) {
            super(itemView);
            img_right = itemView.findViewById(R.id.img_right);
            tv_id_right = itemView.findViewById(R.id.tv_id_right);
            tv_right = itemView.findViewById(R.id.tv_right);
        }
    }

    public interface OnClick{
        void onClick(int position);
    }
    private OnClick onClick;

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }
}
